﻿using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Interop;
using System.Timers;

namespace yearTime {
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window {
        /// <summary>
        /// 当前年份
        /// </summary>
        int curYear = DateTime.Now.Year;
        /// <summary>
        /// 今年总天数
        /// </summary>
        int curYearDays = 365;
        /// <summary>
        /// 定时器
        /// </summary>
        Timer timer;

        private const int WS_EX_TRANSPARENT = 0x20;
        private const int GWL_EXSTYLE = (-20);
        public const int WS_EX_TOOLWINDOW = 0x00000080;

        [DllImport("user32.dll")]
        public static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);

        [DllImport("user32", EntryPoint = "SetWindowLong")]
        private static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint dwNewLong);

        [DllImport("user32", EntryPoint = "GetWindowLong")]
        private static extern uint GetWindowLong(IntPtr hwnd, int nIndex);

        internal class User32 {
            public const int SE_SHUTDOWN_PRIVILEGE = 0x13;

            [DllImport("user32.dll")]
            public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

            [DllImport("user32.dll")]
            public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

            [DllImport("user32.dll")]
            public static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx,
                int cy, uint uFlags);
        }



        public MainWindow() {
            InitializeComponent();
            // SetPenetrate();
            this.SourceInitialized += delegate {
                IntPtr hwnd = new WindowInteropHelper(this).Handle;
                uint extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
                SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_TRANSPARENT);
            };

            try {
                if (Environment.OSVersion.Version.Major < 6) {

                    IntPtr hWndNewParent = User32.FindWindow("Progman", null);
                    User32.SetParent(new WindowInteropHelper(this).Handle, hWndNewParent);
                }
                else {
                    User32.SetWindowPos(new WindowInteropHelper(this).Handle, 1, 0, 0, 0, 0, User32.SE_SHUTDOWN_PRIVILEGE);
                }
            }
            catch (ApplicationException exx) {
                MessageBox.Show(this, exx.Message, "Pin to Desktop");
            }

        }

        private void Window_Loaded(object sender, RoutedEventArgs e) {
            HideInAltTab(this);
            double x1 = SystemParameters.PrimaryScreenWidth;//得到屏幕整体宽度
            double y1 = SystemParameters.PrimaryScreenHeight;//得到屏幕整体高度
            this.Left = x1 - this.Width - 70;
            this.Top = y1 - this.Height - 30;

            UpDataGUI();

            timer = new Timer();
            timer.Elapsed += (timer_sender, timer_e) => {
                UpDataGUI();
            };

            // 1分钟刷新一次
            // 防止休眠后显示时间不正确
            timer.Interval = 1 * 60 * 1000;
            timer.Enabled = true;
            timer.Start();
        }

        /// <summary>
        /// 更新界面
        /// </summary>
        public void UpDataGUI() {
            // 今天是: 2014-04-24 今年还有 100 天
            if ((this.curYear % 4 == 0 && this.curYear % 100 != 0) || this.curYear % 400 == 0) {
                this.curYearDays = 366;
            }

            this.Dispatcher.Invoke(new Action(() => {
                // 更新界面内容
                this.prb.Maximum = this.curYearDays;
                this.prb.Value = DateTime.Now.DayOfYear;
            }));

            StringBuilder sbLblMsgContent1 = new StringBuilder();
            sbLblMsgContent1.Append("今天是: ");
            sbLblMsgContent1.Append(DateTime.Now.ToString("yyyy-MM-dd"));
            sbLblMsgContent1.Append(" 今年还有 ");
            sbLblMsgContent1.Append((this.curYearDays - DateTime.Now.DayOfYear).ToString());
            sbLblMsgContent1.Append(" 天");

            this.Dispatcher.Invoke(new Action(() => {
                this.lblMsg.Content = sbLblMsgContent1.ToString();
                this.lblBFB.Content = (Convert.ToDouble(DateTime.Now.DayOfYear) / Convert.ToDouble(this.curYearDays)).ToString("P");
            }));
        }

        public void HideInAltTab(Window w) {
            /// change the window style to none
            w.WindowStyle = WindowStyle.None;
            w.ShowInTaskbar = false;

            /// get the handle of w
            IntPtr h = new WindowInteropHelper(w).Handle;

            /// set the window extend style to tool window
            SetWindowLong(h, GWL_EXSTYLE, new IntPtr(
                GetWindowLong(h, GWL_EXSTYLE) |
                WS_EX_TOOLWINDOW));
        }

        private void Window_Activated(object sender, EventArgs e) {
            if (Environment.OSVersion.Version.Major >= 6) {
                User32.SetWindowPos(new WindowInteropHelper(this).Handle, 1, 0, 0, 0, 0, User32.SE_SHUTDOWN_PRIVILEGE);
            }
        }
        
    }
}
